<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <link rel="stylesheet" type="text/css" href="../res/static/css/main.css">
  <link rel="stylesheet" type="text/css" href="../res/layui/css/layui.css">
  <script type="text/javascript" src="../res/layui/layui.js"></script>
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
</head>
<body>

  <div class="site-nav-bg">
    <div class="site-nav w1200">
      <p class="sn-back-home">
        <i class="layui-icon layui-icon-home"></i>
        <a href="index.html">首页</a>
      </p>
      <div class="sn-quick-menu">
        <div class="login"><a href="login.html">登录</a></div>
        <div class="sp-cart"><a href="shopcart.html">购物车</a><span>2</span></div>
      </div>
    </div>
  </div>



  <div class="header">
    <div class="headerLayout w1200">
      <div class="headerCon">
        <h1 class="mallLogo">
          <a href="index.html" title="海淘商城">
            <img src="../res/static/img/logo.png">
          </a>
        </h1>
        <div class="mallSearch">
          <form action="" class="layui-form" novalidate>
            <input type="text" name="title" required  lay-verify="required" autocomplete="off" class="layui-input" placeholder="请输入需要的商品">
            <button class="layui-btn" lay-submit lay-filter="formDemo">
                <i class="layui-icon layui-icon-search"></i>
            </button>
            <input type="hidden" name="" value="">
          </form>
        </div>
      </div>
    </div>
  </div>


  <div class="content content-nav-base shopcart-content">
    <div class="main-nav">
      <div class="inner-cont0">
        <div class="inner-cont1 w1200">
          <div class="inner-cont2">
            <a href="commodity.html" class="active">所有商品</a>
            <a href="buytoday.html">今日团购</a>
            <a href="information.html">母婴资讯</a>
            <a href="about.html">关于我们</a>
          </div>
        </div>
      </div>
    </div>
    <div class="banner-bg w1200">
      <h3>夏季清仓</h3>
      <p>宝宝被子、宝宝衣服3折起</p>
    </div>
    <div class="cart w1200">
      <div class="cart-table-th">
        <div class="th th-chk">
          <div class="select-all">
            <div class="cart-checkbox">
              <input class="check-all check" id="allCheckked" type="checkbox" value="true">
            </div>
          <label>&nbsp;&nbsp;全选</label>
          </div>
        </div>
        <div class="th th-item">
          <div class="th-inner">
            商品
          </div>
        </div>
        <div class="th th-price">
          <div class="th-inner">
            单价
          </div>
        </div>
        <div class="th th-amount">
          <div class="th-inner">
            数量
          </div>
        </div>
        <div class="th th-sum">
          <div class="th-inner">
            小计
          </div>
        </div>
        <div class="th th-op">
          <div class="th-inner">
            操作
          </div>
        </div>  
      </div>
      <div class="OrderList">
        <div class="order-content" id="list-cont">
          
        </div>
      </div>


      <!-- 模版导入数据 -->
      <script type="text/html" id="demo">
        {{# layui.each(d.infoList,function(index,item){}}
          <ul class="item-content layui-clear">
            <li class="th th-chk">
              <div class="select-all">
                <div class="cart-checkbox">
                  <input class="CheckBoxShop check" id="{{item.id}}" pid="{{item.p.id}}" type="checkbox" num="all" name="select-all" value="true">
                </div>
              </div>
            </li>
            <li class="th th-item">
              <div class="item-cont">
                <a href="javascript:;"><img src="/file/{{item.p.url}}" alt="{{item.p.pname}}"></a>
                <div class="text">
                  <div class="title">{{item.p.pname}}</div>
                  <p><span>粉色</span>  <span>130</span>cm</p>
                </div>
              </div>
            </li>
            <li class="th th-price">
              <span class="th-su">{{item.p.unitPrice}}</span>
            </li>
            <li class="th th-amount">
              <div class="box-btn layui-clear">
                <div class="less layui-btn">-</div>
                <input class="Quantity-input" type="" name="" value="{{item.num}}" disabled="disabled">
                <div class="add layui-btn">+</div>
              </div>
            </li>
            <li class="th th-sum">
              <span class="sum">{{item.p.unitPrice*item.num}}</span>
            </li>
            <li class="th th-op">
              <span class="dele-btn">删除</span>
            </li>
          </ul>
        {{# });}}
      </script>


      <div class="FloatBarHolder layui-clear">
        <div class="th th-chk">
          <div class="select-all">
            <div class="cart-checkbox">
              <input class="check-all check" id="" name="select-all" type="checkbox"  value="true">
            </div>
            <label>&nbsp;&nbsp;已选<span class="Selected-pieces">0</span>件</label>
          </div>
        </div>
        <div class="th batch-deletion">
          <span class="batch-dele-btn">批量删除</span>
        </div>
        <div class="th Settlement">
          <button id="buy" class="layui-btn">结算</button>
        </div>
        <div class="th total">
          <p>应付：<span class="pieces-total">0</span></p>
        </div>
      </div>
    </div>
  </div>

<script type="text/javascript">
  layui.config({
    base: '../res/static/js/util/' //你存放新模块的目录，注意，不是layui的模块目录
  }).use(['mm','jquery','element','car'],function(){
    var mm = layui.mm,$ = layui.$,element = layui.element,car = layui.car;
    //查询购物车
    /* $.post(
   		"shopcart/findCartByUser.do?uid=1",
   		function(res){
   			$(res).each(function(index){
   				var html='<ul class="item-content layui-clear"><li class="th th-chk"><div class="select-all"><div class="cart-checkbox"><input class="CheckBoxShop check" id="" type="checkbox" num="all" name="select-all" value="true"></div></div></li><li class="th th-item"><div class="item-cont"><a href="javascript:;"><img src="/file/'+this.p.url+'" alt=""></a><div class="text"><div class="title">'+this.p.pname+'</div><p><span>粉色</span><span>130</span>cm</p></div></div></li><li class="th th-price"><span class="th-su">'+this.p.unitPrice+'</span></li><li class="th th-amount"><div class="box-btn layui-clear"><div class="less layui-btn">-</div><input class="Quantity-input" type="" name="" value="'+this.num+'" disabled="disabled"><div class="add layui-btn">+</div></div></li><li class="th th-sum"><span class="sum">'+this.p.unitPrice*this.num+'</span></li><li class="th th-op"><span class="dele-btn">删除</span></li></ul>'
   				$("#list-cont").append($(html));
   			});
			car.init();
   		},"json"
   	); */
   	
	//加载购物车商品种数
	$.post(
		"shopcart/getCount.do?uid=1",
		function(res){
			$(".sp-cart span").html(res);
		},"json"
	);
    
    //结算按钮
    $("#buy").click(function(){
    	$.post(
    		"orders/addOrder.do",
    		{
    			userId:1,//用户id
    			totalMoney:$(".pieces-total").html().substr(1)//应付金额
    		},function(res){
    			if(res>0){//订单添加成功后，添加订单明细
    				var input = $(".CheckBoxShop:checked");//获取选中的商品
    				//对每个勾选商品添加到订单明细表
    				$(input).each(function(index){
    					var that = this;
    					$.post(
    						"orders/addOrderDetail.do",
    						{
    							pid:$(this).attr("pid"),//商品id
    							num:$(this).parent().parent().parent().parent().find(".Quantity-input").val(),//购买数量
    							orderId:res//订单号
    						},function(res){
    							//订单明细添加成功后，删除购物车对应商品
    							$.post(
    								"shopcart/delCart.do?id="+that.id,//购物车id
    								function(res){
    									
    								},"json"
    							);
    						},"json"
    					);
    				});
    			}
    			layer.msg("下单成功！");
    		},"json"
    	); 
    });
    
    
    // 模版导入数据
     var html = demo.innerHTML,
     listCont = document.getElementById('list-cont');
     mm.request({
       url: 'shopcart/findCartByUser.do?uid=1',
       success : function(res){
         listCont.innerHTML = mm.renderHtml(html,res)
         element.render();
         car.init()
       },
       error: function(res){
         console.log(res);
       }
     })
    car.init()  


});
</script>
</body>
</html>